package org.xczy.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xczy.core.entity.Role;
import org.xczy.core.entity.RoleMenu;
import org.xczy.core.mapper.RoleMapper;
import org.xczy.core.mapper.RoleMenuMapper;
import org.xczy.core.service.RoleService;

import javax.annotation.Resource;

/**
* @author Administrator
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2023-05-07 13:34:26
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

  @Resource
  private RoleMapper roleMapper;

  @Resource
  private RoleMenuMapper roleMenuMapper;

  @Override
  @Transactional
  public int addRole(Role role) {
    try {
      role.setRoleId(null);
      role.setRoleState(1);
      return roleMapper.insert(role);
    }catch (Exception e){
      return 0;
    }
  }

  @Override
  public int updateRole(Role role) {
    try{
      return roleMapper.updateById(role);
    }catch (Exception e){
      return 0;
    }
  }

  @Override
  public Role getRoleById(Integer id) {
    try {
      return roleMapper.selectById(id);
    }catch (Exception e){
      return null;
    }
  }

  @Override
  public int deleteRoleById(Integer id) {
    try {
      int i = roleMapper.deleteById(id);
      // 删除权限
      LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(RoleMenu::getRoleId, id);
      roleMenuMapper.delete(wrapper);
      return i;
    }catch (Exception e){
      return 0;
    }
  }


}




